using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;

namespace Jonathan
{
    public partial class fHtmlToolTip : Form, IWebberHost
    {
        private frmMain m_FormMain;
        private Webber m_WebMain;
        private bool m_Drag = false;
        private Point m_Dragpt = new Point(0, 0);
        private string m_Version;
        private string m_VersionPath;

        private const int SW_SHOWNOACTIVATE = 4;
        private const int HWND_TOPMOST = -1;
        private const int SWP_NOACTIVATE = 0x0010;

        [DllImport("User32")]
        private static extern int ShowWindow (int hwnd, int nCmdShow);

        [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
        static extern bool SetWindowPos (
            int hWnd,               // handle to window
            int hWndInsertAfter,    // placement-order handle
            int X,                  // horizontal position
            int Y,                  // vertical position
            int cx,                 // width
            int cy,                 // height
            uint uFlags             // window-positioning options
        );

        public fHtmlToolTip (frmMain main, Point pos, string text, string title, bool close)
        {
            InitializeComponent();

            this.m_FormMain = main;
            this.m_Version = "";
            this.m_VersionPath = "";
            this.m_WebMain = new Webber();
            this.m_WebMain.AllowNavigation = false;
            this.m_WebMain.AllowWebBrowserDrop = false;
            this.m_WebMain.Dock = System.Windows.Forms.DockStyle.Fill;
            this.m_WebMain.innerHTML = null;
            this.m_WebMain.innerTEXT = null;
            this.m_WebMain.IsWebBrowserContextMenuEnabled = false;
            this.m_WebMain.Location = new System.Drawing.Point(0, 0);
            this.m_WebMain.MinimumSize = new System.Drawing.Size(20, 20);
            this.m_WebMain.Name = "m_WebMain";
            this.m_WebMain.TabIndex = 0;
            this.m_WebMain.ScrollBarsEnabled = false;
			this.m_WebMain.SetupWebber(main, this);

            StringBuilder sb = new StringBuilder();
            sb.Append("<html><head><title>ToolTip</title>");
            sb.Append("<style type=\"text/css\">");
            sb.Append("table {margin:1px;font-family: sans-serif, \"Microsoft Sans Serif\", Arial, Verdana, Tahoma; font-size:75%;}");
            sb.Append("a:link {color:black} a:visited {color:black} a:hover {color:black}");
            sb.Append("ul ul { list-style: none; padding-left: 15px; } ul{ margin-left: 0px; padding: 0px; }");
            sb.Append("</style>");
			sb.Append("<script type=\"text/javascript\">function resizeJonathan(){}</script>");
            sb.Append("</head><body style=\"filter:progid:DXImageTransform.Microsoft.Gradient(endColorstr='#CCCCCC', startColorstr='#FFFFFF', gradientType='0');margin:3px;font-size:85%\">");
            sb.Append(text);
            sb.Append("</body></html>");
            this.m_WebMain.HTML = sb.ToString();

            this._btnClose.Visible = close;
            this._pnlTitle.Text = title;
            this._pnlTitle.MouseDown += new MouseEventHandler(Title_MouseDown);
            this._pnlTitle.MouseMove += new MouseEventHandler(Title_MouseMove);
            this._pnlTitle.MouseUp += new MouseEventHandler(Title_MouseUp);

            this.Controls.Add(this.m_WebMain);
            this.m_WebMain.BringToFront();

            this.Height = this.m_WebMain.Document.Body.ScrollRectangle.Height + this._pnlTitle.Height;
            this.Width = this.m_WebMain.Document.Body.ScrollRectangle.Width;

            if (this.Height > 333)
            {
                this.Height = 333;
                this.m_WebMain.ScrollBarsEnabled = true;
            }

            this.Location = pos;
            int left = this.Location.X;
            int top = this.Location.Y;
            int swidth = 0;
            foreach (Screen s in Screen.AllScreens)
                swidth += s.Bounds.Width;
            if (swidth < (left + this.Width))
                left = swidth - this.Width - 5;
            if ((Screen.PrimaryScreen.WorkingArea.Bottom) < (top + this.Height))
                top = Screen.PrimaryScreen.WorkingArea.Bottom - this.Height - 5;

            this.Owner = this.m_FormMain;
            /*SetWindowPos(this.Handle.ToInt32(), HWND_TOPMOST, left,
                top, this.Width, this.Height, SWP_NOACTIVATE);*/
            this.Location = new Point(left, top);
        }

        public void ShowTT ()
        {
            ShowWindow(this.Handle.ToInt32(), SW_SHOWNOACTIVATE);
        }

        void Title_MouseUp (object sender, MouseEventArgs e)
        {
            this.m_Drag = false;
        }

        void Title_MouseMove (object sender, MouseEventArgs e)
        {
            if (m_Drag)
            {
                this.Top = MousePosition.Y - m_Dragpt.Y;
                this.Left = MousePosition.X - m_Dragpt.X;
            }
        }

        void Title_MouseDown (object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                m_Dragpt.X = e.X;
                m_Dragpt.Y = e.Y;
                m_Drag = true;
            }
        }

        /// <summary>
        /// Gets the Title of the Tool tip...
        /// </summary>
        public string Title
        {
            get { return this._pnlTitle.Text; }
        }

        private void _btnClose_Click (object sender, EventArgs e)
        {
            this.Close();
        }

        private void _mnuctxClose_Click (object sender, EventArgs e)
        {
            this.Close();
        }

        #region IWebberHost Members

		public bool SelectRefWord (string word, bool tryContains)
        {
			return true;
        }

        public ContextMenuStrip GetContextMenu ()
        {
            System.Windows.Forms.ContextMenuStrip ctx = new ContextMenuStrip();
            //ctx.Opening += new CancelEventHandler(ctx_Opening);

            // Copy...
            ToolStripMenuItem tsmiCopy = new ToolStripMenuItem("Copy");
            tsmiCopy.Click += new EventHandler(Copy_Click);
            // Select All...
            ToolStripMenuItem tsmiSelAll = new ToolStripMenuItem("Select All");
            tsmiSelAll.Click += new EventHandler(SelAll_Click);

            ctx.Items.Add(tsmiCopy);
            ctx.Items.Add(tsmiSelAll);

            return ctx;
        }

        void Copy_Click (object sender, EventArgs e)
        {
            this.m_WebMain.Copy();
        }

        void SelAll_Click (object sender, EventArgs e)
        {
            this.m_WebMain.SelectAll();
        }

        public string Book
        {
            get { return ""; }
        }

        public string GetVersion (int version)
        {
            return this.m_Version;
        }

        public void SetVersion (string version)
        {
            this.m_Version = version;
        }

        public string GetVersionPath (int version)
        {
            return this.m_VersionPath;
        }

        public void SetVersionPath (string versionPath)
        {
            this.m_VersionPath = versionPath;
            this.m_Version = this.m_FormMain.GetDBName(versionPath);
        }

        /// <summary>
        /// Gets the TabberPage associated with the host...
        /// </summary>
        /// <returns>TabberPage</returns>
        public Tabber.TabberPage GetTabPage ()
        {
            return null;
        }

        #endregion
    }
}